package pri.damai.xiaowu.usercenter.api.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import pri.damai.xiaowu.common.core.form.IdForm;
import pri.damai.xiaowu.common.core.util.LocalUserUtils;
import pri.damai.xiaowu.common.core.vo.QueryResultCommonVO;
import pri.damai.xiaowu.common.core.vo.ResultVO;
import pri.damai.xiaowu.common.log.anno.XiaoWuLog;
import pri.damai.xiaowu.usercenter.sysauth.command.UserResourceQuery;
import pri.damai.xiaowu.usercenter.sysauth.entity.SysResource;
import pri.damai.xiaowu.usercenter.sysauth.service.impl.SysResourceServiceImpl;
import pri.damai.xiaowu.usercenter.vo.ResourceVO;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

import static pri.damai.xiaowu.common.core.vo.ResultVO.success;

/**
 * 系统资源表(SysResource)表控制层
 * @Desc
 * @Author DaMai
 * @Date 2022-03-03 17:53:58
 */
@Api(tags = "用户权限")
@XiaoWuLog
@RestController
@RequestMapping("userResource")
public class SysResourceController {
    /**
     * 服务对象
     */
    @Resource
    private SysResourceServiceImpl sysResourceService;

    @PostMapping("/getResourceTree")
    public ResultVO<List<ResourceVO>> selectAll(@Valid @RequestBody IdForm idForm) {

        List<ResourceVO> allMenus = sysResourceService.getAllMenusBySysId(idForm.getId());

        return success(allMenus);
    }

    @PostMapping("/getUserResource")
    public ResultVO<List<ResourceVO>> getUserResource(@Valid @RequestBody UserResourceQuery query) {

        Long userId = LocalUserUtils.getLocalUser().getUserId();
        query.setUserId(userId);

        List<ResourceVO> data = sysResourceService.getResourceBySysIdAndUserId(query);

        return success(data);
    }

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param sysResource 查询实体
     * @return 所有数据
     */
    @GetMapping
    public ResultVO<QueryResultCommonVO<SysResource>> selectAll(Page<SysResource> page, SysResource sysResource) {
        Page<SysResource> resourcePage = this.sysResourceService.page(page, new QueryWrapper<>(sysResource));
        return success(QueryResultCommonVO.buildByIPage(resourcePage));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResultVO<SysResource> selectOne(@PathVariable Serializable id) {
        return success(this.sysResourceService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sysResource 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ResultVO<Boolean> insert(@RequestBody SysResource sysResource) {
        return success(this.sysResourceService.save(sysResource));
    }

    /**
     * 修改数据
     *
     * @param sysResource 实体对象
     * @return 修改结果
     */
    @PutMapping
    public ResultVO<Boolean> update(@RequestBody SysResource sysResource) {
        return success(this.sysResourceService.updateById(sysResource));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public ResultVO<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        return success(this.sysResourceService.removeByIds(idList));
    }
}
